package csu.web.mypetstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import csu.web.mypetstore.mapper.DeliveryOrderMapper;
import csu.web.mypetstore.pojo.DeliveryOrder;
import csu.web.mypetstore.service.AccountService;
import csu.web.mypetstore.service.DeliveryOrderService;
import csu.web.mypetstore.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 18632
* @description 针对表【delivery_order】的数据库操作Service实现
* @createDate 2025-03-19 23:01:55
*/
@Service
public class DeliveryOrderServiceImpl extends ServiceImpl<DeliveryOrderMapper, DeliveryOrder>
    implements DeliveryOrderService{
    @Autowired
    private DeliveryOrderMapper deliveryOrderMapper;
    @Autowired
    private AccountService accountService;

    @Override
    public void insertOrder(DeliveryOrder deliveryOrder) {
         deliveryOrderMapper.insert(deliveryOrder);
    }

    @Override
    public Result deleteOrder(String username,int orderId) {
      Wrapper<DeliveryOrder> wrapper = new LambdaQueryWrapper<DeliveryOrder>()
               .eq(DeliveryOrder::getOrderid, orderId);
        deliveryOrderMapper.delete(wrapper);
        return Result.success(getOrderByUsername(username));
    }

    @Override
    public List<DeliveryOrder> getOrderByUsername(String username) {
        //TODO: get user id from username
        //int userId=accountService.getUserId(username);

        int userId=accountService.getUserId(username);
        Wrapper<DeliveryOrder> wrapper = new LambdaQueryWrapper<DeliveryOrder>()
               .eq(DeliveryOrder::getUserid, userId);
        return deliveryOrderMapper.selectList(wrapper);
    }

    @Override
    public List<DeliveryOrder> getAllOrders() {
        return deliveryOrderMapper.selectList(null);

    }
}




